package fzrj.qkl.demo.controller;

import fzrj.qkl.demo.model.CommonResponse;
import fzrj.qkl.demo.service.backend.CarrierBackendService;
import org.fisco.bcos.sdk.transaction.model.exception.ContractException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/carrier")
public class CarrierController {

    @Autowired
    private CarrierBackendService carrierBackendService;

    // 承运商注册
    @PostMapping(value = "/add")
    public CommonResponse addCarrier(@RequestParam String name, @RequestParam String info, @RequestParam String passwd) {

        boolean ok = carrierBackendService.addCarrier(name, info, passwd);
        if (ok) {
            return CommonResponse.ok(ok);
        }

        return CommonResponse.fail("4001",new Exception("error"));
    }

    // 承运商验证
    @PostMapping("/check")
    public CommonResponse checkCarrier(@RequestParam int id, @RequestParam String passwd) throws ContractException {

        boolean ok = carrierBackendService.checkCarrier(id, passwd);
        if (ok) {
            return CommonResponse.ok(ok);
        }

        return CommonResponse.fail("4001",new Exception("error"));
    }

    // 获取承运商的运单列表
    @PostMapping("/shipments")
    public CommonResponse getShipmentsByCarrier(@RequestParam int id) throws ContractException {

        Object shipments = carrierBackendService.getShipmentsByCarrier(id);
        return CommonResponse.ok(shipments);
    }

}
